'use strict';

app.controller('interestCtrl', ['$scope','$http','loginService','adminService', function($scope,$http,loginService,adminService){

	
 $scope.init = function() { 
	     loginService.islogged(); 
		 $scope.link = loginService.getLink();
		 $scope.type_filter_event = 0;
		 $scope.entryLimit = 30;
		 $scope.offset = 0;
		 $scope.disablePrev = true;
		 $scope.disableNext = false;
		 $scope.page_now = 1;
		 $scope.nbr_rows=0;
		 $scope.visible_most = true;
		 $scope.type_filter= 'all';
	    $scope.list_interests();
		//  $scope.list_interests_populaire();
		 
		    
	     //=====================event combox filter===============================
		 $scope.filter_interest = [{
		      "interest": "all interest",
		    	  "val": 1
		    }, 
		    { "interest": "popular interest",
		    	"val": 2
		    }];
		 
		 $scope.selectedOption = $scope.filter_interest[1];
		 
		//========================================================================
		 
	    };
	
	 $scope.currentPage = 1; //current page
	 
	  //=====================paging list member of groups======================
	    $scope.entryLimit_group = 2
	    $scope.offset_group = 0;
	    $scope.disablePrev_group = true;
	    $scope.disableNext_group = false;
	    $scope.page_now_group = 1;
	    //================================================================
	 
	    //=====================paging list groups of user======================
	    $scope.entryLimit_group_ = 5;
	    $scope.offset_group_ = 0;
	    $scope.disablePrev_group_ = true;
	    $scope.disableNext_group_ = false;
	    $scope.page_now_group_ = 1;
	    //================================================================
	 
	 var show_list_group = 1;
	 var show_group_detail = 0;
	 var show_user_detail = 0;
	 
	   $scope.change = function() {
	    	var val = $scope.filter.val;
	    	$scope.type_filter_event = val;
			
			if(val==1){
	    	 $scope.list_interests();
			 $scope.type_filter= 'all';
			 $scope.visible_most = true;
	        }
		    else{
			  $scope.list_interests_populaire();
			  $scope.type_filter= 'most';
			  $scope.visible_most = false;
			}
	    	$scope.disablePrev = true;
	    	$scope.page_now=1;
	    	$scope.offset =0;
	    	
	    	if($scope.filter_ini <= $scope.entryLimit){ 
	    		$scope.disableNext = true;
	    	}
	    	
	    }
	 
	 $scope.del = function(interest){
        var uid = loginService.getUser(); 
		adminService.delete_interest(uid.username,uid.password,interest);	
      }
	 
	 
	  $scope.list_group_visible = function (id) {
		    return show_list_group == id;
	 };  
	 
	 
	  $scope.group_detail_visible = function (id) {
	        return show_group_detail == id;
	  };  
	 
	  
	  $scope.return_group_detail = function(user,id) {
		  if ( $scope.type_return ==1){
			show_list_group = 1;
			show_group_detail = 0;    
		  }
		  else if ( $scope.type_return ==2){
			  show_user_detail = 1;
			  show_group_detail = 0;
			  show_list_group = 0;
			  $scope.type_return = 1;
			  $scope.go_user_detail($scope.user_id,1);
		  }
		};
	  
		
		$scope.return_ = function(user,id) {
			if ( $scope.type_return ==1){
				show_user_detail = 0;
				show_group_detail = 1;
			    $scope.list_interests();
			}
			
			else if ( $scope.type_return ==2){
				//show = 1;
				show_group_detail = 1;	
				show_user_detail = 0;
				$scope.type_return = 1;
			}
			
		};	
	  
	
		$scope.filter_group = function () { 
			$scope.filtered_search = null;
			$scope.filtered_search = angular.copy($scope.filtered);
			if($scope.nb_row_notchange == $scope.filtered.length)
			   $scope.filtered_ini = angular.copy($scope.filtered);
		 
			if( !angular.isUndefined($scope.search1) ) { 
			
				if( !angular.isUndefined($scope.filtered_ini) )  { 
					$scope.recalculate_array($scope.filtered_ini);
					$scope.disablePrev = true;
				}
				$scope.filter_ini = $scope.filtered.length;
			}
			
         }	
		
		$scope.$watch('a', function() {   
			if( ($scope.page_now==1)  &&  ($scope.nbr_rows <= $scope.entryLimit) )
			 {
			 $scope.disableNext = true;
			 }
			 if($scope.type_filter== 'most'){ 
				  $scope.disableNext = true;
			 }
			
		});
		
		$scope.$watch('b', function() {   
			if( ($scope.page_now_group==1)  &&  ($scope.nbr_rows_groups <= $scope.entryLimit_group) )
			 {
			 $scope.disableNext_group = true;
			 }
			
		}); 
		
		$scope.$watch('c', function() {   
			if( ($scope.page_now_group_==1)  &&  ($scope.nbr_rows_members <= $scope.entryLimit_group_) )
			 {
			 $scope.disableNext_group_ = true;
			 }
			
		}); 
		
		  $scope.$watch('filtered', function() {
			  if( !angular.isUndefined($scope.search1) ) { 
					$scope.filter_ini = $scope.filtered.length;
				}
			  
			  if($scope.search1 ==''){  
					$scope.filter_ini = $scope.nb_row_notchange;
				}
			  
		
			 if( ($scope.page_now==1)  &&  ($scope.filter_ini <= $scope.entryLimit) )
				 { 
					 if(!angular.isUndefined($scope.search1)){  //search has some thing
					    if(!$scope.click_prev)
					    	$scope.disableNext = true;
					}
//					 if($scope.filter_ini <= $scope.entryLimit)
//						 $scope.disableNext = true;
				 }
 			 else if(($scope.page_now==1) &&  ($scope.filter_ini > $scope.entryLimit)){
 				  
				   $scope.disableNext = false; 
 				
			 }
			 

				 if( ($scope.page_now==$scope.noOfPages)  &&  ($scope.filter_ini <= $scope.entryLimit) )
		 				$scope.disablePrev = true;
			     else if(($scope.page_now==$scope.noOfPages) &&  ($scope.filter_ini > $scope.entryLimit)){
						 $scope.disablePrev = false;
			     }

			 
				 
			 if(($scope.page_now!=1) && ($scope.page_now!=$scope.noOfPages)){ 
			    if ( $scope.filter_ini < $scope.entryLimit){ //alert($scope.filter_ini);
					 $scope.disableNext = true;
                     //$scope.disablePrev = true;
				 }
				 else{
					 $scope.disableNext = false;
					 //$scope.disablePrev = false;
				 }
			 }
			 
		   });
		
	
		 $scope.list_interests = function(){
			 $http.get($scope.link+'lst_interest').success(function(data){
				
					console.log('load list of interests');
					$scope.model = data;
					$scope.jsonToArray(data);
					//pageResult($scope);
                       		
					$scope.nb_row_notchange =  data.data.length;
					$scope.noOfPages = Math.ceil($scope.nb_row_notchange/$scope.entryLimit);
					
				})
				.error(function(data) {
					console.log('can not load list of interests');
			    });
		 }
		 
		 
		  $scope.list_interests_populaire = function(){
            $scope.a = new Array();
            for (var j = 0; j < 10; j++) {  
	          $scope.a[j] = new Array(1);
		    }			
		   
		    $scope.a[0][0] =  'Fishing';
			$scope.a[1][0] =  'Bowling';
			$scope.a[2][0] =  'Collecting';
			$scope.a[3][0] =  'Biking';
			$scope.a[4][0] =  'Reading';
			$scope.a[5][0] =  'Running';
			$scope.a[6][0] =  'Music';
			$scope.a[7][0] =  'Traveling';
			$scope.a[8][0] =  'Movies';
			$scope.a[9][0] =  'Shopping';
			 $scope.disableNext = true;
		  /*
			 $http.get($scope.link+'users').success(function(data){
				$scope.model1=data;
				$scope.getInterest_populaire(data);
					
				})
				.error(function(data) {
					console.log('can not load list of users');
			    });
				*/
		 }
		
		 
	 $scope.getInterest_populaire = function (data) {  
		
		var i = 0;
		var k = 0;
		$scope.a = new Array();
		angular.forEach(data.data,function(){
			//if(!angular.isUndefined(data.data[i].interests)){ 
			if(data.data[i].interests){ 
				var interests = data.data[i].interests;
				var interests_Str = interests.toString();
				var arr  = interests_Str.split(",");  
				if(arr.length>1){ 
					for (var j = 0; j < arr.length; j++) {  
						$scope.a[k]=new Array(1);
						$scope.a[k][0] =  arr[j];
						
						k++;
					} 
					
				}else{ 
					$scope.a[k]=new Array(1);
					$scope.a[k][0] = interests_Str;
				}
				 k++;
				 i++;
			}//end if
        })
		
		 $scope.a = unique($scope.a);
		
         $scope.nbr_rows = k ;
		 $scope.filter_ini = k ;
		 $scope.a_ini= angular.copy($scope.a);
		 
		 $scope.nb_row_notchange =  k;
		 $scope.noOfPages = Math.ceil($scope.nb_row_notchange/$scope.entryLimit);
		//alert(JSON.stringify($scope.a));
	
			
	}
		 
		
	  $scope.jsonToArray = function (data) { //alert(JSON.stringify( data));
			$scope.a = new Array();
			var i = 0;
			angular.forEach(data.data,function(){
				$scope.a[i]=new Array(2);
				$scope.a[i][0] =  data.data[i].name;
				$scope.a[i][1] =  data.data[i].id;
	            i++;
	        })
	        $scope.nbr_rows = i ;
			$scope.filter_ini = i ;
			$scope.a_ini= angular.copy($scope.a);
	       //alert(JSON.stringify( $scope.a));
		}
	  
	  
	  
	  $scope.recalculate_array = function (data) { 
			 $scope.a = new Array();
			 var i = 0;
			 angular.forEach(data,function(){
				 $scope.a[i]=new Array(3);
				 var splitted =  ((data[i]).toString()).split(',') ;
				// alert(splitted[0]);
			      $scope.a[i][0] =  splitted[0];
			      $scope.a[i][1] =  splitted[1];
				  $scope.a[i][2] =  splitted[2];
				 i++;

	         })
	         $scope.nbr_rows = i ;
			 $scope.a_ini= angular.copy($scope.a);
		 }
	  
	  
	  $scope.nextPage = function() {
		  nextPage_group($scope);
	  }
	  
	  $scope.previousPage = function() {
		  previousPage_group($scope);
	  }
	 

	
}])